;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; SEND_TO_TX_BUFFER_STACK_MACRO PUTS w INTO THE NEXT AVAILABLE TX SLOT
SEND_TO_TX_BUFFER_STACK_MACRO	MACRO
	LOCAL SND_TO_TX_BUFF_STACK_1
	LOCAL SND_TO_TX_BUFF_STACK_2
	LOCAL SND_TO_TX_BUFF_STACK_3
	LOCAL SND_TO_TX_BUFF_STACK_4
	LOCAL SND_TO_TX_BUFF_STACK_5
	LOCAL DONE_SND_TO_TX_BUFF_STACK
	;DISABLE INTERRUPTS TO NOT CORRUPT THE BUFFER
	BCF INTCON,GIE,A
	MOVF	TX_STATE_0,F,A			;SEE IF BUFFER ALREADY HAS DATA
	BNZ		SND_TO_TX_BUFF_STACK_1
	MOVWF	TX_STATE_0,A
	BRA		DONE_SND_TO_TX_BUFF_STACK
	
SND_TO_TX_BUFF_STACK_1:
	MOVF	TX_STATE_1,F,A			;SEE IF BUFFER ALREADY HAS DATA
	BNZ		SND_TO_TX_BUFF_STACK_2
	MOVWF	TX_STATE_1,A
	BRA		DONE_SND_TO_TX_BUFF_STACK
	
SND_TO_TX_BUFF_STACK_2:
	MOVF	TX_STATE_2,F,A			;SEE IF BUFFER ALREADY HAS DATA
	BNZ		SND_TO_TX_BUFF_STACK_3
	MOVWF	TX_STATE_2,A
	BRA		DONE_SND_TO_TX_BUFF_STACK
	
SND_TO_TX_BUFF_STACK_3:
	MOVF	TX_STATE_3,F,A			;SEE IF BUFFER ALREADY HAS DATA
	BNZ		SND_TO_TX_BUFF_STACK_4
	MOVWF	TX_STATE_3,A
	BRA		DONE_SND_TO_TX_BUFF_STACK
	
SND_TO_TX_BUFF_STACK_4:
	MOVF	TX_STATE_4,F,A			;SEE IF BUFFER ALREADY HAS DATA
	BNZ		SND_TO_TX_BUFF_STACK_5
	MOVWF	TX_STATE_4,A
	BRA		DONE_SND_TO_TX_BUFF_STACK
	
SND_TO_TX_BUFF_STACK_5:
	MOVWF	TX_STATE_5,A

DONE_SND_TO_TX_BUFF_STACK
	BSF INTCON,GIE,A	;RE-ENABLE INTERRUPTS
	ENDM
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; SND_DAT_TO_TX_BUFF_STACK_MACRO PUTS w AND MACRO_DATA INTO THE NEXT AVAILABLE TX SLOT
SND_DAT_TO_TX_BUFF_STACK_MACRO	MACRO
	LOCAL SND_DAT_TO_TX_BUFF_STACK_1
	LOCAL SND_DAT_TO_TX_BUFF_STACK_2
	LOCAL SND_DAT_TO_TX_BUFF_STACK_3
	LOCAL SND_DAT_TO_TX_BUFF_STACK_4
	LOCAL SND_DAT_TO_TX_BUFF_STACK_5
	LOCAL DONE_SND_DAT_TO_TX_BUFF_STAC
	;DISABLE INTERRUPTS TO NOT CORRUPT THE BUFFER
	BCF INTCON,GIE,A
	
	MOVF	TX_STATE_0,F,A			;SEE IF BUFFER ALREADY HAS DATA
	BNZ		SND_DAT_TO_TX_BUFF_STACK_1
	MOVWF	TX_STATE_0,A
	MOVFF	MACRO_DATA,TX_CHAR_0
	;NOP
	BRA		DONE_SND_DAT_TO_TX_BUFF_STAC
	
SND_DAT_TO_TX_BUFF_STACK_1:
	MOVF	TX_STATE_1,F,A			;SEE IF BUFFER ALREADY HAS DATA
	BNZ		SND_DAT_TO_TX_BUFF_STACK_2
	MOVWF	TX_STATE_1,A
	MOVFF	MACRO_DATA,TX_CHAR_1
	;NOP
	BRA		DONE_SND_DAT_TO_TX_BUFF_STAC
	
SND_DAT_TO_TX_BUFF_STACK_2:
	MOVF	TX_STATE_2,F,A			;SEE IF BUFFER ALREADY HAS DATA
	BNZ		SND_DAT_TO_TX_BUFF_STACK_3
	MOVWF	TX_STATE_2,A
	MOVFF	MACRO_DATA,TX_CHAR_2
	;NOP
	BRA		DONE_SND_DAT_TO_TX_BUFF_STAC
	
SND_DAT_TO_TX_BUFF_STACK_3:
	MOVF	TX_STATE_3,F,A			;SEE IF BUFFER ALREADY HAS DATA
	BNZ		SND_DAT_TO_TX_BUFF_STACK_4
	MOVWF	TX_STATE_3,A
	MOVFF	MACRO_DATA,TX_CHAR_3
	;NOP
	BRA		DONE_SND_DAT_TO_TX_BUFF_STAC
	
SND_DAT_TO_TX_BUFF_STACK_4:
	MOVF	TX_STATE_4,F,A			;SEE IF BUFFER ALREADY HAS DATA
	BNZ		SND_DAT_TO_TX_BUFF_STACK_5
	MOVWF	TX_STATE_4,A
	MOVFF	MACRO_DATA,TX_CHAR_4
	;NOP
	BRA		DONE_SND_DAT_TO_TX_BUFF_STAC
	
SND_DAT_TO_TX_BUFF_STACK_5:
	MOVWF	TX_STATE_5,A
	MOVFF	MACRO_DATA,TX_CHAR_5
	;NOP

DONE_SND_DAT_TO_TX_BUFF_STAC:
	BSF INTCON,GIE,A	;RE-ENABLE INTERRUPTS
	ENDM
	
;;;;;;;;;;;;;;;;;;;;;; ROTATE TX BUFFERS ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ROTATE_TX_BUFFER_STACK	MACRO	;6 TO 21 INSTRUCTIONS
	local DONE_ROTATING_TX_BUFFER
	;DISABLE INTERRUPTS TO NOT CORRUPT THE BUFFER
	BCF INTCON,GIE,A
	
	MOVF	TX_STATE_1,W,A
	MOVWF	TX_STATE_0,A
	BZ		DONE_ROTATING_TX_BUFFER
	MOVFF	TX_CHAR_1,TX_CHAR_0
	;NOP
	MOVF	TX_STATE_2,W,A
	MOVWF	TX_STATE_1,A
	BZ		DONE_ROTATING_TX_BUFFER
	MOVFF	TX_CHAR_2,TX_CHAR_1
	;NOP
	MOVF	TX_STATE_3,W,A
	MOVWF	TX_STATE_2,A
	BZ		DONE_ROTATING_TX_BUFFER
	MOVFF	TX_CHAR_3,TX_CHAR_2
	;NOP
	MOVF	TX_STATE_4,W,A
	MOVWF	TX_STATE_3,A
	BZ		DONE_ROTATING_TX_BUFFER
	MOVFF	TX_CHAR_4,TX_CHAR_3
	;NOP
	MOVFF	TX_STATE_5,TX_STATE_4
	MOVFF	TX_CHAR_5,TX_CHAR_4
	CLRF	TX_STATE_5,A
	;NOP
DONE_ROTATING_TX_BUFFER:
	BSF INTCON,GIE,A	;RE-ENABLE INTERRUPTS
	ENDM
